package com.dats.hot;

import com.dats.util.BytesUtil;
import com.dats.util.LogUtil;

import java.io.File;

/**
 * @author sig
 * @since 2016/9/25
 */
public abstract class AbstractHotSwap {

    public abstract void setHotSwap(HotSwapI hotSwap);

    public boolean reload(String classname) {
        try {
            byte[] bytes = BytesUtil.file2Bytes(new File(
                    "hot/" + classname + ".class"));
            HotSwapClassLoader loader = new HotSwapClassLoader();
            Class<?> clazz = loader.defineClass(bytes);

            if (null == clazz) return false;
            HotSwapI hotSwap = (HotSwapI) clazz.newInstance();
            setHotSwap(hotSwap);
        } catch (Exception e) {
            LogUtil.console.error("Oh no," + classname + " reload failed.");
            return false;
        }
        return true;
    }
}
